/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * <p>Project: app2022 - D1
 * <p>Powered by webrx On 2022-02-10 09:05:16
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class D1 {

    @Test
    @DisplayName("随机LocalDate")
    public void m11() {
        var start = LocalDate.of(2021, 1, 1);
        var end = LocalDate.now();
        var rand = new Random();
        System.out.println(start);

        //随机日期
        var y = rand.nextInt(start.getYear(), end.getYear() + 1);
        var m = rand.nextInt(1, 13);
        var d = rand.nextInt(1, 32);
        var rd = LocalDate.of(y, m, d);
        System.out.println(rd);
        System.out.println(end);

    }

    @Test
    @DisplayName("Scanner")
    public void m10() {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        System.out.println(a * b);
    }

    @Test
    @DisplayName("格式化")
    public void m9() {
        System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("e")));
        System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("E")));
        System.out.println(LocalDate.now().getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA));
        System.out.println(LocalDate.now().getMonth().getDisplayName(TextStyle.FULL, Locale.CHINA));
    }

    @Test
    @DisplayName("获取某月第一天")
    public void m8() {
        var n = LocalDate.now();
        System.out.println(n);
        System.out.println(n.with(TemporalAdjusters.firstDayOfMonth()));//本月第一天日期 2022-02-01
        System.out.println(n.with(TemporalAdjusters.lastDayOfMonth()));//2022-02-28
        System.out.println(n.with(TemporalAdjusters.lastDayOfYear()));//2022-12-31
        System.out.println(n.plusMonths(3).with(TemporalAdjusters.lastDayOfMonth()));//2022-05-31

        //2008-08-08
        var d = LocalDate.parse("20080808", DateTimeFormatter.ofPattern("yyyyMMdd"));
        System.out.println(d.isLeapYear());//true 2008 是闰年

        Year year = Year.now();
        System.out.println(year.getValue());//2022
        year = Year.parse("2020");
        System.out.println(year.isLeap());//true
        System.out.println(year.getValue());//2020

        for (int i = 0; i < 12; i++) {
            var m = Month.of(i + 1);
            var mdays = LocalDate.now().isLeapYear() && m.getValue() == 2 ? m.maxLength() : m.minLength();
            System.out.println(String.format("%s有%d天", m.getDisplayName(TextStyle.FULL, Locale.CHINA), mdays));
        }
    }

    @Test
    @DisplayName("LocalDateTime2")
    public void m7() {
        var t = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //计算两个日期有多少天
        var d1 = LocalDate.now();
        var d2 = LocalDate.of(2006, 10, 20);
        //1方式
        var days = d2.until(d1, ChronoUnit.DAYS);
        System.out.println(days);

        //LocalDateTime
        var du = Duration.between(d2.atStartOfDay(), d1.atStartOfDay());
        //2方式
        System.out.println(du.toDays());
        //3方式
        var days3 = ChronoUnit.DAYS.between(d2, d1);
        System.out.println(days3);

        var p = Period.between(d2, d1);
        System.out.printf("%s到%s，间隔%d年%d月%d天%n", d2.atStartOfDay().format(t), d1.atStartOfDay().format(t), p.getYears(), p.getMonths(), p.getDays());
        //2006-10-20 00:00:00到2022-02-10 00:00:00，间隔15年3月21天
        System.out.println(ChronoUnit.MONTHS.between(d2, d1));
        System.out.println(ChronoUnit.YEARS.between(d2, d1));


    }


    @Test
    @DisplayName("LocalDateTime2")
    public void m6() {
        //var n = LocalDateTime.now(ZoneId.of("GMT+0"));
        var n = LocalDateTime.now();
        var f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(n.format(f));

        //
        //System.out.println(LocalDateTime.now().getNano());
        //System.out.println(LocalDateTime.now().getNano());


        //秒
        System.out.println(n.toEpochSecond(ZoneOffset.of("+8")));

        Instant i = n.toInstant(ZoneOffset.of("+8"));

        //毫秒
        System.out.println(n.toInstant(ZoneOffset.of("+8")).toEpochMilli());
        long time = n.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        System.out.println(System.currentTimeMillis());

        //转换为Date
        System.out.println(new Date(time));
    }


    @Test
    @DisplayName("LocalDateTime")
    public void m5() {
        var n = LocalDateTime.now();// LocalDate + LocalTime 相当于 java.util.Date
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        System.out.println(n.format(df));
        System.out.println(df.format(n));
        System.out.println(n.format(DateTimeFormatter.ofPattern("MM月dd日")));
        LocalDate d = n.toLocalDate();
        LocalTime t = n.toLocalTime();
    }

    @Test
    @DisplayName("LocalTime")
    public void m4() {
        LocalTime time = LocalTime.now();
        time = LocalTime.of(15, 30, 25);

        time = LocalTime.now(ZoneId.of("GMT+0"));
        time = LocalTime.now(ZoneId.of("GMT+8"));
        System.out.println(time);
        time = LocalTime.now(ZoneId.of("Asia/Shanghai"));
        System.out.println(time.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
        time = time.plusMinutes(15);
        System.out.println(time);
        time = time.minusMinutes(20);
        System.out.println(time);
    }


    @Test
    @DisplayName("LocalDate运算")
    public void m3() {
        var d = LocalDate.now();
        //d = d.plusDays(35);//日期增加35
        //d = d.plus(25, ChronoUnit.MONTHS);
        d = d.plusDays(-5);
        System.out.println(d);
        System.out.println(d.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));
        System.out.println(d.getMonth().getDisplayName(TextStyle.FULL, Locale.CHINA));

        //将字符串转换为LocalDate
        var d2 = LocalDate.parse("1985-11-23", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(d2);
        DateTimeFormatter df = DateTimeFormatter.ofPattern("MM月dd日");
        System.out.println(d2.format(df));
        System.out.println(df.format(d2));


    }


    @Test
    @DisplayName("java 1.8 LocaleDate")
    public void m2() {
        //LocalDate
        LocalDate d1 = LocalDate.now();
        System.out.println(d1);
        System.out.printf("%tF", d1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println();

        LocalDate d2 = LocalDate.of(1985, 10, 25);
        //System.out.println(LocalDate.MIN);
        //System.out.println(LocalDate.MAX);
        System.out.println(d2);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        System.out.println(dtf.format(d2));
        //Month
        //DayOfWeek
        System.out.println(d2.isLeapYear()); //判断是不是闰年,是返回true
        System.out.println(d2.getDayOfWeek().name());
        System.out.println(d2.getDayOfMonth());//25
        System.out.println(d2.getDayOfYear());//298
        System.out.println(d2.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA));
        System.out.println(d2.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.CHINA));
        System.out.println(d2.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.US));
        System.out.println(d2.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.US));

        System.out.println(d2.getMonth().getDisplayName(TextStyle.FULL, Locale.CHINA));
        System.out.println(d2.getMonth().getDisplayName(TextStyle.SHORT, Locale.CHINA));
        System.out.println(d2.getMonth().getDisplayName(TextStyle.FULL, Locale.US));
        System.out.println(d2.getMonth().getDisplayName(TextStyle.SHORT, Locale.US));
        System.out.println(d2.getMonth().getDisplayName(TextStyle.FULL, Locale.TAIWAN));
        System.out.println(d2.getMonth().getDisplayName(TextStyle.SHORT, Locale.TAIWAN));
    }

    @Test
    @DisplayName("时间戳,Date,SimpleDateFormat,Calendar")
    public void m1() {
        //System.out.println("ok");
        System.out.println(System.currentTimeMillis());
        //1644455260353 long
        System.out.printf("%tF %<tT", 1644455260353L);
        System.out.println();
        System.out.printf("%tF %<tT%n", 0L);
        System.out.printf("%tF %<tT%n", Long.MAX_VALUE);
        //9999-12-31 23:59:59
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(new Date()));
        try {
            System.out.println(sdf.parse("9999-12-31 23:59:59").getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //时间戳转Date,再使用SimpleDateFormat 格式化为字符串
        System.out.println(sdf.format(new Date(253402271999000L)));

        Calendar c = Calendar.getInstance();
        //Calendar转Date
        Date d2 = c.getTime();

        //Calendar转换为long时间戳
        long times = c.getTimeInMillis();

        c.set(Calendar.YEAR, 9999);
        c.set(Calendar.MONTH, 12 - 1);
        c.set(Calendar.DAY_OF_MONTH, 31);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        System.out.println(c.getTimeInMillis());
        System.out.printf("%tF %<tT%n", c);
    }
}
